// ==UserScript==
// @name General ShortCuts
// @match *://*/*
// @run-at document-start
// ==/UserScript==

/*!
 *  @author  xiaody <xiaody09@163.com>
 *  @description  HotKeys.js
 */

(function(){
"use strict";

if( location.pathname.substr(0, 13) === '/reader/view/' )
	return;

var prevStr = ['上一页','<上一页','上页','<<上页','[上一页]','<前页','‹ 前一页','上一篇','« 上一页',
                'Prev','< Previous','Previous','Previous Page','← Previous page','<','‹','<<','«','‹‹','◄',
				'Previous photo','← Previous photo'],
	nextStr = ['下一页','下一页>','下页','下页>>','[下一页]','后页>','下一页 ›','下一篇','下一页 »',
                'Next','Next >','Next »','Next Page','Next Page »','Next page →','>','›','>>','»','››','►',
				'Next photo','Next photo →'],
	fKey=[0,112,113], BACKSPACE=8, ESC=27, HOME=36, D=68, F=70, BACKSLASH=220,
	hovered = null;

function keydownCheck(e) { // The main function.
	"use strict";
	var KeyID = e.keyCode;
	if ( document.activeElement === document.body && !e.shiftKey && !e.ctrlKey ) {
		//e.stopPropagation();
		switch ( KeyID ) {
			case F:
				gonext(); return;
			case D:
				goprev(); return;
			case BACKSPACE:
				e.preventDefault();
				e.stopPropagation();
				return;
		}
	}
	switch ( KeyID ) {
		case fKey[1]:
			e.preventDefault();
			history.back(); break;
		case fKey[2]:
			history.forward(); break;
		case HOME:
			if(e.ctrlKey){
				e.preventDefault();
				top.location.href = top.location.origin;
			} break;
		case BACKSLASH:
			if(e.ctrlKey){
				var inp = getfirstInputbox();
				if(inp&&inp!==document.activeElement) {inp.focus();inp.value = inp.value;}
				else if(inp&&inp===document.activeElement) inp.select();
			} break;
		case ESC:
			var _tagname = hovered.tagName, _parent = hovered.parentNode;
			if(_tagname==='A'){
				e.preventDefault();e.stopPropagation();
				toggleLink(hovered);
			}else if( ('SPAN,EM,FONT,B,STRONG'.match(_tagname))&&_parent.tagName==='A' ){
				e.preventDefault();e.stopPropagation();
				toggleLink(_parent);
			} break;
	}

}

function xpath( query ) {
	'use strict';
	var ret = [];
    var xRet = document.evaluate( query, document, null, 6, null );
    for ( let i = xRet.snapshotLength; i--; )
		ret[i] = xRet.snapshotItem(i);
    return ret.length ? ret : null;
}

function getfirstInputbox(){
	'use strict';
    var inputs = document.getElementsByTagName('input');
	for(let i=0, l=inputs.length; i<l; i++){
		if(inputs[i].getAttribute('type')!=='hidden')
			return inputs[i];
    }
	return null;
}

function goprev(){
	for(let s, aa, i=0, l=prevStr.length; i<l; i++){
		s = prevStr[i];
		aa = "//a[text()='" + s + "']|//a/*[text()='" + s + "']/..";
		if(xpath(aa)) { xpath(aa)[0].click();return true }
	}
	return false;
}
function gonext(){
	for(let s, aa, i=0, l=nextStr.length; i<l; i++){
		s = nextStr[i];
		aa = "//a[text()='" + s + "']|//a/*[text()='" + s + "']/..";
		if(xpath(aa)) { xpath(aa)[0].click();return true }
	}
	return false;
}

function toggleLink(ele){ // Turn a link into plain text temporarily.
	var _backup = ele.cloneNode(true);
	_backup.style.opacity = .1;
	ele.removeAttribute('href');
	ele.removeAttribute('onclick');
	ele.removeAttribute('onmouseover');
	ele.style.cssText += 'color:DarkSlateGrey;cursor:text;';
	setTimeout(function(){
		ele.parentNode.replaceChild(_backup, ele);
		fadeIn(_backup);
	}, 12800);
}
function fadeIn(el){ // Fade in an HTMLElement.
	var _inter = function(){
		var _o = + el.style.opacity;
		if(_o >> 0) {el.style.opacity = 1; return;}
		else webkitRequestAnimationFrame(_inter);
		el.style.opacity = _o + .02;
	};
	webkitRequestAnimationFrame(_inter);
}

document.addEventListener('keydown', keydownCheck, true);
document.addEventListener('mouseover', function(e){ hovered = e.target }, true);

})();

/*!
 *  @author  xiaody <xiaody09@163.com>
 *  @description  MouseGesture.js
 */

(function() {
"use strict";
var mousemiddle = 0, mousewheel = 0, MidTimeOut;

function downcheck(e) {
	"use strict";
	var button = e.button;
	if( e.target.tagName !== 'A' && e.target.parentNode.tagName !== 'A'&& !e.ctrlKey && !e.shiftKey){
		switch ( button ) {
			case 0:
				if ( mousemiddle ) {
					e.preventDefault();
					$scroll(0);
				}
				break;
			case 1:
				clearTimeout( MidTimeOut );
				if ( !mousemiddle ) {
					e.preventDefault();
					e.stopPropagation();
					mousemiddle = 1;
				} else {
					mousemiddle = 0;
				}
				break;
			case 2:
				if ( mousemiddle ) {
					e.preventDefault();
					$scroll( document.body.scrollHeight - window.innerHeight );
				}
				break;
		}
	}
}
function upcheck(e) {
	"use strict";
	var button = e.button;
	if ( e.target.tagName !== 'A' && !e.ctrlKey && !e.shiftKey ) {
		switch ( button ) {
			case 0:
				var MidTimeOut = setTimeout(function(){mousemiddle = 0}, 5);
				break;
			case 1:
				var MidTimeOut = setTimeout(function(){mousemiddle = 0}, 600);
				break;
			case 2:
				var MidTimeOut = setTimeout(function(){mousemiddle = 0}, 20);
				break;
		}
	}
}
function wheelcheck(e) {
	"use strict";
	if ( mousemiddle ) {
		switch ( e.wheelDelta ) {
			case 120:
				if ( mousewheel !== 1 ) {
					mousewheel = 1;
					setTimeout( function(){mousewheel = 0}, 200 );
				} else if (history.length === 1 && mousewheel === 1) {
					setTimeout( function(){window.open('', '_self').close()}, 100 );
				} else {
					setTimeout( function(){history.go(-1)}, 100 );
					mousewheel = 0;
				}
				break;
			case -120:
				if ( mousewheel !== -1 ) {
					mousewheel = -1;
					setTimeout( function(){mousewheel = 0}, 200 );
				} else {
					setTimeout( function(){history.go(1)}, 100 );
					mousewheel = 0;
				}
				break;
		}
		e.preventDefault();
		e.stopPropagation();
	}
}

function $scroll( y ) { // Smooth scroll to (0, y).
	"use strict";
	var tail = y > document.body.scrollTop ? 10 : -10;
	var inter = function() {
		var _beforeStep = document.body.scrollTop;
		window.scrollBy( 0, (y - _beforeStep)*.36 + tail );
		if ( document.body.scrollTop !== _beforeStep )
			webkitRequestAnimationFrame( inter );
	};
	webkitRequestAnimationFrame( inter );
}

document.addEventListener( 'contextmenu', function(e){ if(mousemiddle) e.preventDefault()}, true );
document.addEventListener( 'mousedown', downcheck, true );
document.addEventListener( 'mouseup', upcheck, true );
document.addEventListener( 'mousewheel', wheelcheck, true );

})();

/*!
 *  @author  xiaody <xiaody09@163.com>
 *  @description  Scripts for my topSites.
 */

const hostname = location.hostname;

if ( hostname === "commondatastorage.googleapis.com" && location.pathname.match( /chromium-browser-(\w+?)\/(.+)\/LAST_CHANGE/i ) )
	document.addEventListener( 'DOMContentLoaded', function() {
		var pre = document.getElementsByTagName('pre')[0];
		var build = RegExp.$1,
			os = RegExp.$2;
		var ver = pre.textContent;
		var href = 'https://commondatastorage.googleapis.com/chromium-browser-' + build + '/index.html?path=' + os + '\/' + ver +'\/';
		pre.innerHTML = '<a href="' + href +'">' + ver + '</a>';
	}, true );
